	
#ifndef __READER_SAMAV3_H__
#define __READER_SAMAV3_H__


typedef enum sam_key_type_e{
	SAM_KEY_T_DESFire4 = 0,
	SAM_KEY_T_ISO_TDEA16 = 1,
	SAM_KEY_T_MIFARE = 2,
	SAM_KEY_T_3TDEA = 3,
    SAM_KEY_T_AES_128 = 4,
	SAM_KEY_T_AES_192 = 5,
	SAM_KEY_T_ISO_TDEA32 = 6,
    SAM_KEY_T_AES_256 = 7,
    SAM_KEY_T_AES_128LRP = 8,
	SAM_KEY_T_MAX,
}sam_key_type_t;

typedef enum sam_key_class_e{
    SAM_KEY_CLASS_HOST = 0x00,
    SAM_KEY_CLASS_PICC = 0x01,
    SAM_KEY_CLASS_OfflineChange = 0x02,
    SAM_KEY_CLASS_OfflineCrypto = 0x04,
    SAM_KEY_CLASS_OfflineUpload = 0x05,
    SAM_KEY_CLASS_OfflinePerso = 0x06,
}sam_key_class_t;

typedef enum sam_key_id_e{
    SAM_KID_KM      = 0x00,     //Master Key
	SAM_KID_KFR0	= 0x01,     //默认读密钥ID

	SAM_KID_KFR		= 0x02,     //工厂读密钥ID
	SAM_KID_KFW		= 0x03,     //工厂写密钥ID，模块不需要
	SAM_KID_KFM		= 0x04,     //工厂发行信息MAC密钥ID
    SAM_KID_KFRM    = 0x05,     //工厂数据信息MAC密钥ID

	SAM_KID_KMR		= 0x11,     //厂商读密钥ID
	SAM_KID_KMW		= 0x12,     //厂商写密钥ID
	SAM_KID_KMM		= 0x13,     //厂商发行信息MAC密钥ID
	
	SAM_KID_KCR		= 0x14,     //客户化读密钥ID
	SAM_KID_KCW		= 0x15,     //客户化写密钥ID
	SAM_KID_KCM		= 0x16,	    //客户化发行信息MAC密钥ID

    SAM_KID_KMRM    = 0x17,     //厂商数据信息MAC密钥ID
    SAM_KID_KCRM    = 0x18,     //客户化数据信息MAC密钥ID

	SAM_KID_KUR		= 0x21,     //用户读密钥ID
	SAM_KID_KUW		= 0x22,     //用户写密钥ID
	SAM_KID_KUM		= 0x23,	    //用户发行信息MAC密钥ID
    SAM_KID_KURM    = 0x24,     //用户数据信息MAC密钥ID

	SAM_KID_APPLE_PRIV	= 0x31, //苹果priv密钥ID
	SAM_KID_APPLE_CRED	= 0x32, //苹果cred密钥ID

	SAM_KID_GOOGLE		= 0x34, //谷歌密钥ID
	SAM_KID_GOOGLE_MAC	= 0x35, //谷歌MAC密钥ID
	
	SAM_KID_LEAF		= 0x36, //Leaf密钥ID
	SAM_KID_LEAF_MAC	= 0x37, //Leaf MAC密钥ID

    SAM_KID_SAMSUNG_PAY = 0x38, //samsung pay密钥ID
    SAM_KID_SAMSUNG_PAY_MAC = 0x39, //samsung pay MAC密钥ID 
    SAM_KID_SW_AWC_APP = 0x3A, //sw awc app密钥ID
    SAM_KID_SW_SDC_READ = 0x3B, //
    SAM_KID_ACCESS_GRID_APPLE_READ = 0x3C,
    SAM_KID_ACCESS_GRID_APPLE_PRIVACY = 0x3D,
}sam_key_id_t;

#pragma pack(1)
//SAMAV3密钥存储结构
typedef struct {
    uint8_t keys[3][16];
    uint8_t aid[3];
    uint8_t key_no;
    uint8_t kce_no,kce_ver;
    uint8_t kuc_no;
    uint16_t set;
    uint8_t key_ver[3];
    uint16_t ext_set;
    uint8_t kae_no,kae_ver;
}sam_key_entry_t;

typedef struct{
    uint8_t ver[3];
    uint8_t aid[3];
    uint8_t key_no;
    uint8_t key_no_cek;
    uint8_t key_v_cek;
    uint8_t ref_no_kuc;
    uint16_t set;
    uint16_t ext_set;
    uint8_t key_no_aek;
    uint8_t key_v_aek;
}key_entry_read_t;
#pragma pack()

//初始化samav3
int samav3_init(void);

//激活samav3
int samav3_activate(void);

//读取samav3密钥
int samav3_get_key_entry(uint8_t kid, key_entry_read_t *key_entry);

//装载samav3密钥
int samav3_change_key_entry(uint8_t kid, sam_key_entry_t *key_entry);

//samav3密钥恢复默认
int samav3_disable_key_entry(uint8_t kid);

//装载密钥
int samav3_load_key(uint8_t kid, const uint8_t key[16], uint8_t kver, uint8_t kclass, uint8_t ktype, uint8_t keep_iv);
int samav3_load_key_if_changed(uint8_t kid, const uint8_t key[16], uint8_t kver, uint8_t kclass, uint8_t ktype, uint8_t keep_iv);
int samav3_load_default_key(uint8_t kid, const uint8_t key[16], uint8_t kver, uint8_t kclass, uint8_t ktype, uint8_t keep_iv);

//校验MAC
int samav3_verify_mac_div(uint8_t kid, uint8_t key_ver, uint8_t* div, uint8_t div_len, uint8_t* data, uint16_t dlen);

//samav3下电
int samav3_deactivate(void);

int samav3_activate2(void);

int samav3_init2(void);

void samav3_get_uid(uint8_t* uid, uint8_t *uid_len);

int samav3_is_ready(void);

int samav3_is_active(void);

#endif // !__READER_SAMAV3_H__


